
function jsJihuahuankuan(txfl, fl, hkze, dt1, dt2, zdhk) {


    //var txfl = document.getElementById("txfl").value - 0;                                       ////单笔提现费率
    //var fl = document.getElementById("fv").value - 0;            ////消费费率
    //var hkze = document.getElementById("max").value - 0;          ////还款总额
    //var dt1 = document.getElementById("dt1").value;          ////起始时间
    //var dt2 = document.getElementById("dt2").value;          ////结束时间
    //var zdhk = document.getElementById("min").value - 0;          ////最少钱数
    var date1 = new Date(Date.parse(dt1.replace(/-/g, "/")));
    var date2 = new Date(Date.parse(dt2.replace(/-/g, "/")));

   // document.getElementById("nr").innerHTML = "";


    var paysinfo = getpaysinfo(fl - 0, txfl - 0, hkze - 0, zdhk - 0, 0, 0, 0, []);
    console.info(paysinfo);
    if (paysinfo == -1) {
        alert("该策略不可能还款");
        return;
    }
    //  console.info(paysinfo)

    var zongshouxufei = paysinfo.sxf;

    var sumall = zongshouxufei + hkze;
    var times = paysinfo.tim; //console.info(sumall);
    var avrage = Math.floor(sumall / times);

    if (avrage >= Math.ceil(zdhk - zongshouxufei)) {

        times += 1;
        avrage = Math.floor(sumall / times);
    }
    if (avrage < 100 || zdhk - zongshouxufei < 100) {
        //document.getElementById("zh").innerHTML = "";
        alert("该策略可能导致消费金额不足100，请修改参数");
        return;
    }
    var rage = getMems(avrage, Math.ceil(zdhk), hkze, times, 0, paysinfo.list);
    if (rage == -1) {
        // document.getElementById("zh").innerHTML = "";
        alert("该策略可能无法还款，请重新计算一次，或者增加预备金或者降低还款额");
        return;
    }

    var ts = timeFn22(dt1, dt2, rage.length);
    console.info(ts);
    if (ts == null) {
        alert("每天还款超过三笔，已超出银行预警，请更换策略！");
        return;
    }
    //console.info(ts);
    //  return;


    // document.getElementById("nr").innerHTML = "";

    return mainSearch(rage, fl, txfl, zdhk, ts);
}



    function timeFn22(d1,d2,tims) {//di作为一个变量传进来 
        //如果时间格式是正确的，那下面这一步转化时间格式就可以不用了 
        var h = -1;
        var dateBegin = new Date(Date.parse(d1.replace(/-/g, "/")));//将-转化为/，使用new Date 
        var dateEnd = d2 ? new Date(Date.parse(d2.replace(/-/g, "/"))) : new Date();//获取当前时间 
        if (isToday22(d1) == null) {
            return null;/////////////////////////不能选择过去！
        } else if (isToday22(d1) == true) {
            dateBegin = new Date();
            h = dateBegin.getHours();
            dateBegin.setTime(dateBegin.getTime() + 24 * 60 * 60 * 1000);
               
            // dateBegin = new Date().setHours(0, 0, 0, 0);
        }
        dateBegin.setHours(0, 0, 0, 0);
        dateEnd.setTime(dateEnd.getTime() + 24 * 60 * 60 * 1000);
        dateEnd.setHours(0, 0, 0, 0);

        var dateDiff = dateEnd.getTime() - dateBegin.getTime();//时间差的毫秒数 
        var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000));//计算出相差天数 
        var leave1 = dateDiff % (24 * 3600 * 1000); //计算天数后剩余的毫秒数 
        var hours = Math.floor(leave1 / (3600 * 1000));//计算出小时数 
        //计算相差分钟数 
        var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数 
        var minutes = Math.floor(leave2 / (60 * 1000));//计算相差分钟数 
        //计算相差秒数 
        var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数 
        var seconds = Math.round(leave3 / 1000);
           
        var todaysstimes = 0;
        var sjlist = [];
        if (tims / dayDiff < 2) {
            // console.info(1);
        } else {
            //  console.info(2);
            if (h > -1) {
                if (h <= 15) {
                    todaysstimes = 2;
                    //  console.info(3);
                } else {
                    if (h < 19) {
                        todaysstimes = 1;
                        // console.info(4);
                    }

                }
                //  console.info(5);
                if (todaysstimes>0) {
                    tims -= todaysstimes;

                    var ddd = rtnRndTime22(h, 21, todaysstimes, 0, dateBegin);
                    for (var i = 0; i < ddd.length; i++) {
                        sjlist.push(ddd[i]);
                    }
                }
                 
            }

        }

        var numsss = Math.ceil(tims / dayDiff);
        if (numsss > 3) {
            return null;
        }
        //console.info(dayDiff);
        var ddd2;
        for (var i = 0; i < dayDiff; i++) {
            ddd2 = rtnRndTime22(7, 21, numsss, i + 1, dateBegin);
            for (var t = 0; t < ddd2.length; t++) {
                sjlist.push(ddd2[t]);
            }
        }

        //console.info(sjlist);
        // var lefttimes = tims - todaysstimes;
            

        return sjlist;
    }

function getdate22() {
    var arr = new Array();
    var startTime = new Date('2018', '01', '01').getTime();
    var endTime = new Date('2018', '01', '31').getTime();
    for (var i = startTime; i <= endTime;) {
        arr.push(formatTime(i, '-'));
        i += 24 * 60 * 60 * 1000;
    }
    return arr;
}




function rnd22(n, m) {
    var random = Math.floor(Math.random() * (m - n + 1) + n);
    return random;
}
function retntime22(ad, hourst, hourend) {
    // console.info(dateBegin);
    var dateBegin = new Date();
    dateBegin.setTime(ad);


    return dateBegin.getFullYear() + "-" + (dateBegin.getMonth() - (0 - 1)) + "-" + dateBegin.getDate() + " " + rnd22(hourst, hourend) + ":" + rnd22(0, 50) + ":" + rnd22(0, 60);
    //return new Date(dateBegin.getFullYear(), dateBegin.getMonth() + 1, 1, randomNum(hourst, hourend), randomNum(0, 50), randomNum(0, 60), randomNum(0, 999));

}

function allday22(str, end, num, day, dateBegin) {
    var avlu = dateBegin.getTime();
    avlu += day * 24 * 60 * 60 * 1000;
    //dateBegin.setTime(dateBegin.getTime() + day * 24 * 60 * 60 * 1000);
    if (num == 1) {
        return [retntime22(avlu, 15, 20)];
    } else if (num == 2) {
        return [retntime22(avlu, 12, 14), retntime22(avlu, 18, 20)];
    } else if (num == 3) {
        return [retntime22(avlu, 8, 10), retntime22(avlu, 12, 14), retntime22(avlu, 18, 20)];
    } else {
               
    }

}

function rtnRndTime22(str, end, num, day, dateBegin) {

    if (day > 0) {
        var ds = dateBegin;
        return allday22(str, end, num, day, ds);
    }
    dateBegin.setTime(dateBegin.getTime() - 24 * 60 * 60 * 1000);
    var avlu = dateBegin.getTime();
    if (num == 1) {
              
        return [retntime22(avlu, 19, 21)];
              
    } else if (num == 2) {
        return [retntime22(avlu, 15, 17), retntime22(avlu, 18, 20)];
    }
            
    return sss;

}
function isToday22(str) {
    var d = new Date(str.replace(/-/g, "/"));
          
    var todaysssDate = new Date();
           
    if (d.setHours(0, 0, 0, 0) == todaysssDate.setHours(0, 0, 0, 0)) {
        return true;
    } else {
        if (d.setHours(0, 0, 0, 0) < todaysssDate.setHours(0, 0, 0, 0)) {
            return null;
        }
        return false;
    }
}







function getpayByPay(fl, sxf, pay, res, tim) {
    var charge = pay * fl + sxf;/////手续费
    var surePay = pay - charge; ////实际到账
    var may = res - surePay;   ////应还款
    return { res: may, canpay: surePay, charge: charge, all: pay, tim: tim };/////剩余、可支付、手续费、实际支出、进入次数
}
function getpayByRes(fl, sxf, pay, res, tim) {
    var charge = (res + sxf) / (1 - fl) - res;
    var surePay = res;
    var may = res - surePay;
    return { res: may, canpay: pay - charge, charge: charge, all: (res + sxf) / (1 - fl), tim: tim };/////剩余、可支付、手续费、实际支出、进入次数
}
function getpaysinfo(fl, txfl, hkze, zdhk, jrcs, zzc, zsxf, fllist) {/////费率，提现费率，还款总额，最低还款，进入次数，总支出，总手续费
    var a;
    if (hkze * (1 - fl) + 1 < zdhk) {
        a = getpayByRes(fl, txfl, zdhk, hkze, jrcs);
        fllist.push(a.charge);
    } else {
        a = getpayByPay(fl, txfl, zdhk, hkze, jrcs);
        fllist.push(a.charge);
    }
    if (a.canpay <= 0) {
        return -1;
    }
    hkze = a.res;
    zdhk = a.canpay;
    zzc += a.all;
    zsxf += a.charge;
    if (a.res <= 0) {
        return { list: fllist, sxf: zsxf, zzc: zzc, pyliang: zdhk - a.all, tim: jrcs + 1 };
    } else {
        return getpaysinfo(fl, txfl, hkze, zdhk, jrcs + 1, zzc, zsxf, fllist);
    }

}
function getFen(i) {
    return Math.ceil(i * 100)

}
function neartime(dl,l) {
    var dateBegin = new Date(Date.parse(dl.replace(/-/g, "/"))).getTime();//将-转化为/，使用new Date 
    var dateBegin2 = new Date().getTime();
    var tms = (dateBegin + dateBegin2) / 2;
    var sdsa = new Date();
    sdsa.setTime(tms);
    var d = [];
    d.push(sdsa.getFullYear() + "-" + (sdsa.getMonth() - (0 - 1)) + "-" + sdsa.getDate() + " " + (sdsa.getHours()) + ":" + sdsa.getMinutes() + ":" + sdsa.getSeconds());
    return d;
}
function timeChaifen(d1,num) {
    var dateBegin = new Date(Date.parse(d1.replace(/-/g, "/")));//将-转化为/，使用new Date 
    var d = [];
    //  return dateBegin.getFullYear() + "-" + (dateBegin.getMonth() - (0 - 1)) + "-" + dateBegin.getDate() + " " + rnd22(hourst, hourend) + ":" + rnd22(0, 50) + ":" + rnd22(0, 60);


    for (var i = num; i >0; i--) {
        d.push(dateBegin.getFullYear() + "-" + (dateBegin.getMonth() - (0 - 1)) + "-" + dateBegin.getDate() + " " + (dateBegin.getHours()-i-1) + ":" + rnd22(0, 50) + ":" + rnd22(0, 60));
    }
            
    return d;


}
function canchaifen(d1) {
    var dateBegin = new Date(Date.parse(d1.replace(/-/g, "/"))).getTime();//将-转化为/，使用new Date 
    var ggg2 = new Date().getTime();
    if (dateBegin - ggg2 > 3 * 60 * 60 * 1000) {
        return true;
    } return false;
}
            
          

function mainSearch(rage, fl, txfl, zdhk,ts) {
    // var totle = 0;
    // console.info(ts);
    var totlefei = 0;
    var rtn = 0;

    var dddddd = [];
    var xfcs = 0;
    var txcs = 0;
    for (var i = 0; i < rage.length; i++) {
        var zsxf = getpayByResNum(rage[i], fl, txfl);//////总手续费
        var xf = zsxf + rage[i];//////消费
        var rg = [];
        var tims = [];
        if (xf > 220) {/////////////////////////////////////////是否拆分消费，如果消费金额大于200，拆，如果时间较短，不拆
            if (canchaifen(ts[i])) {
                if (xf < 2000) {
                    rgs = randomNum(xf > 1000 ? xf / 2 : 100, xf > 1000 ? 1000 : xf - 100);

                } else {
                    rgs = randomNum(900, 1000);
                }
                tims = timeChaifen(ts[i], 2);
                rg.push(rgs);
                rg.push(xf - rgs);
            } else {
                rg.push(xf);
                tims = neartime(ts[i], 1);
            }

        } else {
            if (canchaifen(ts[i])) {
                tims = timeChaifen(ts[i], 1);
            } else {
                tims = neartime(ts[i], 1);
            }
                   
            rg.push(xf);
        }
        for (var t = 0; t < rg.length; t++) {
            var xf2 = rg[t].toFixed(2);////////////消费
            var xf2fl = (getFen(rg[t] * fl) / 100).toFixed(2);////////////手续费
            xfcs++;
            totlefei += (xf2fl - 0);
            totlefei = totlefei.toFixed(2)-0;
            zdhk -= xf2;
            dddddd.push({ money: 0 - xf2, sxf: xf2fl, times: tims[t], syje: toFixed2(zdhk, 2) });
            //document.getElementById("nr").innerHTML +=
            //    "<div style='border: 1px solid red;margin: 2px;'>消费：<span style='color:red'>" +
            //    xf2 + "</span>手续费：<span style='color:red'>" +
            //xf2fl + "</span>当前余额:<span style='" + (zdhk < 0 ? "color:red;" : "") + "'>" + tims[t] +"</span></div>";
        }
        txcs++;
        totlefei += txfl;
        rtn += rage[i];
        zdhk += rage[i];
        dddddd.push({ money: rage[i], sxf: txfl, times: ts[i], syje: toFixed2(zdhk, 2) });
        //document.getElementById("nr").innerHTML +=
        //    "<div style='border: 1px solid green;margin: 2px;'>还款：<span style='color:green'>" +
        //toFixed2(rage[i], 2) + "</span>手续费：<span style='color:green'>" + toFixed2(txfl, 2) + "</span>当前余额:<span style='" + (zdhk < 0 ? "color:red;" : "") + "'>" + ts[i] +"</span></div>";
    }

    return { arr: dddddd, xfcs: xfcs, hkcs: txcs, gjsxf: totlefei };
    //document.getElementById("zh").innerHTML = "共计还款：" + rtn.toFixed(2) + "手续费：" + totlefei + "合计：" + (rtn.toFixed(2)-0 + totlefei-0);

}
function jisuanfeilv(pay, fl) {
    return 0;//pay * fl > (Math.floor(pay * fl * 100) / 100) ? 0.01 : 0;
}
function getpayByPayNum(pay, fl, sxf) {
    var charge = pay * fl + sxf;/////手续费
    return Math.ceil(charge * 100) / 100;/////剩余、可支付、手续费、实际支出、进入次数
}
function getpayByResNum(wtpay, fl, sxf) {
    var charge = (wtpay + sxf) / (1 - fl) - wtpay;
    return Math.ceil(charge * 100) / 100;
}
function getrandom(minnum, maxnum, tatol, size, list, tim) {
    tim = tim ? tim : 1;
    if (tim > 10) {

        return [-1];
    }
    var num = tatol; //定义整数
    var ccmax = maxnum;

    var length = size;  //定义多个整数的数量
    var numArr = [];
    var i = 0;
    while (length > 1) {
        maxnum -= list[i];
        minnum = minnum > maxnum ? maxnum : (minnum < 100 ? 100 : minnum);

        //minnum = minnum < maxnum ? (minnum + maxnum) / 2 : minnum;
        //minnum -= list[i];
                
        var rnd22 = randomNum(minnum, maxnum); //生成随机数
        // console.log(minnum + "--"+rnd22+"--" + maxnum);
        num -= rnd22;
        numArr.push(rnd22);
        i += 1;
        length--;
    }
           
    numArr.push(toFixed2(num, 2) - 0)
    numArr = numArr.sort(function (x, y) {
        return y - x;
    });

    numArr = numArr.sort(function (x, y) {
        return y - x;
    });

    if (num > maxnum) {
        return getrandom(minnum, ccmax, tatol, size, list, tim + 1);
    }
    if (numArr[numArr.length - 1] < 100) {
        return turn(numArr);
    }
    //console.info(numArr);
    return numArr;
}
function turn(numArr) {
    if (numArr[numArr.length - 1] < 100) {
        numArr[0] = toFixed2(numArr[0] - (100 - numArr[numArr.length - 1]), 2) - 0;
        numArr[numArr.length - 1] = 100;
    }
    numArr = numArr.sort(function (x, y) {
        return y - x;
    });
    if (numArr[numArr.length - 1] < 100) {
        return turn(numArr);
    } else {
        return numArr;
    }
}

function toFixed2(num, s) {
    var times = Math.pow(10, s)
    var des = num * times + 0.5;
    des = parseInt(des, 10) / times
    return des + ''
}
function getMems(min, max, all, num, tim,list) {
    if (tim) {
        tim++;
        if (tim > 10) {
            return -1;
        }
    }
    var dd = getrandom(min, max, all, num,list);
    if (dd[0] > max || dd[0] < 0) {
        return -1;
    }
    //var sd = true;
    //for (var i = 0; i < dd.length; i++) {
    //    if ((dd[i] < min || dd[i] > max) && i != (dd.length - 1)) {
    //        sd = false;
    //    }
    //    if (i == (dd.length - 1) && dd[i] > max) {
    //        sd = false;
    //    }
    //}
    //if (sd == false) {


    //    return getMems(min,max , all, num + 1, tim + 1);
    //}
    //else {
    return dd;
    // }
}


function randomNum(minNum, maxNum) {
    switch (arguments.length) {
        case 1:
            var dd = parseInt(Math.random() * minNum + 1, 10);
            dd = Math.floor(dd / 10) * 10;
            // dd = parseInt((dd / 10).toFixed(0)) * 10
            return dd;
            break;
        case 2:
            var dd = parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
            dd = Math.floor(dd / 10) * 10;
            //dd = parseInt((dd / 10).toFixed(0)) * 10
            return dd;
            break;
        default:
            return 0;
            break;
    }
}



